import {getPlaylistTrackAll, getSongUrl, getSongDetail} from "@/network/songs/musicrelated";
import {ElMessage} from "element-plus";
import {getCookie} from "@/until/cookie";

/*es5歌单去重方法，以后改进*/
function unique(arr: any) {
    for (let i = 0; i < arr.length; i++) {
        for (let j = i + 1; j < arr.length; j++) {
            if (arr[i].id === arr[j].id) {         //第一个等同于第二个，splice方法删除第二个
                arr.splice(j, 1);
                j--;
            }
        }
    }
    return arr;
}

interface stateInterface {
    playlist: []
}

export default {
    namespaced: true,
    state: {
        playlist: []
    },
    mutations: {
        /*将整理好的歌单数据保存到歌单中*/
        PROCESS_PLAYLIST(state: stateInterface, value: any) {
            state.playlist = value;
            console.log(state.playlist)
        },
        // ADD_SINGLE: function (state: stateInterface, value: any) {
        //     state.playlist.unshift(value);
        //     //去重
        //     state.playlist = unique(state.playlist);
        // },
        /*删除歌单中指定的单曲*/
        DELETE_THIS(state: stateInterface, value: any) {
            state.playlist.splice(value.$index, 1);
        }
    },
    actions: {
        /*整理歌单的数据*/
        async processPlaylist(context: any, value: any) {
            const {data:res} = await getPlaylistTrackAll(value.id,getCookie());

            const allId = res.songs.map((item: any) => item.id).join(",");
            if (res.songs.length === 0) {
                ElMessage({
                    message: "请先登陆",
                    grouping: true,
                    type: "warning"
                });
            }
            const {data:allUrls} = await getSongUrl(allId,getCookie());

            const informationNeeded = res.songs.map((item: any) => {
                return {
                    id: item.id,
                    picUrl: item.al.picUrl,
                    name: item.name,
                    ar: item.ar.map((item: any) => item.name).join(" "),
                    arId: item.ar.map((item: any) => item.id),
                    url: allUrls.data.filter((element: any) => element.id === item.id)[0].url,
                    /*控制删除弹出框的显示与否，不重要的属性*/
                    deletePopover: false
                };
            });
            context.commit("PROCESS_PLAYLIST", informationNeeded);
        },
        /*整理单曲的数据*/
        // async getPlaylistDetail(context:any, value:any) {
        //     const {data: res} = await getSongDetail(value.id);
        //     const {data: allUrls} = await getSongUrl(value.id);
        //     const singleInformationNeeded = res.songs.map((item:any) => {
        //         return {
        //             id: item.id,
        //             picUrl: item.al.picUrl,
        //             name: item.name,
        //             ar: item.ar.map((item:any) => item.name).join(" "),
        //             arId: item.ar.map((item:any) => item.id),
        //             url: allUrls.data.filter((element:any) => element.id === item.id)[0].url,
        //             /*控制删除弹出框的显示与否，不重要的属性*/
        //             deletePopover: false
        //         };
        //     });
        //     context.commit("ADD_SINGLE", ...singleInformationNeeded);
        // }
    },
    modules: {}
};
